package com.example.javaee.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.javaee.common.result;
import com.example.javaee.entity.AssetCategory;
import com.example.javaee.entity.AssetLabel;
import com.example.javaee.entity.AssetOrg;
import com.example.javaee.service.AssetCategoryService;
import com.example.javaee.service.AssetOrgService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/assetCategory")
public class AssetCategoryController {
    @Autowired
    private AssetCategoryService assetCategoryService;

    //获取所有安全组织
    @GetMapping("/getAll")
    public result<List<AssetCategory>> getAll(HttpServletRequest request) {
        LambdaQueryWrapper<AssetCategory> queryWrapper = new LambdaQueryWrapper<>();
        // 执行查询
        List<AssetCategory> assetCategoryList = assetCategoryService.list(queryWrapper);
        return result.success(assetCategoryList,"获取成功");

    }

    //获取标签的名字
    @GetMapping("/getByid/{id}")
    public result<AssetCategory> getByid(HttpServletRequest request,@PathVariable Integer id) {
        // 执行查询
        AssetCategory assetCategory= assetCategoryService.getById(id);
        return result.success(assetCategory,"获取成功");
    }
    //添加标签
    @PostMapping("/add")
    public  result<String> register(@RequestBody AssetCategory assetCategory){
        LambdaQueryWrapper<AssetCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AssetCategory::getCategoryName, assetCategory.getCategoryName());
        if (assetCategoryService.getOne(queryWrapper)!=null){
            return result.error("标签已存在");
        }
        assetCategoryService.save(assetCategory);
        return result.success(null,"添加成功");

    }

    @DeleteMapping("/{id}")
    public result<String> delete(@PathVariable Integer id) {
        if(assetCategoryService.removeById(id)){
            return  result.success(null,"删除成功");}
        else{
            return result.error("删除失败");
        }
    }
}
